#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/11/8 16:28
@desc
"""

import datetime
from dateutil.relativedelta import relativedelta


def getdate(attr, mode, offset):
    """
    计算偏移量的时间
    :param attr:
    1. Year
    2. Month
    3. Day
    4. Hours
    5. Minutes
    6. Seconds
    7. Microseconds
    :param mode: add sub
    :param offset: integer
    :return:
    """
    current = datetime.date.today()
    if mode == 'add':
        if attr == "Year" or attr == 1:
            dateoffset = current + relativedelta(years=+offset)
        elif attr == "Month" or attr == 2:
            dateoffset = current + relativedelta(months=+offset)
        elif attr == "Day" or attr == 2:
            dateoffset = current + relativedelta(days=+offset)
        elif attr == "Hours" or attr == 3:
            dateoffset = current + relativedelta(hours=+offset)
        elif attr == "Minutes" or attr == 4:
            dateoffset = current + relativedelta(minutes=+offset)
        elif attr == "Seconds" or attr == 5:
            dateoffset = current + relativedelta(seconds=+offset)
        elif attr == "Microseconds" or attr == 6:
            dateoffset = current + relativedelta(microseconds=+offset)
        else:
            raise Exception('输入的属性不正确')
    elif mode == 'sub':
        if attr == "Year" or attr == 1:
            dateoffset = current - relativedelta(years=+offset)
        elif attr == "Month" or attr == 2:
            dateoffset = current - relativedelta(months=+offset)
        elif attr == "Day" or attr == 2:
            dateoffset = current - relativedelta(days=+offset)
        elif attr == "Hours" or attr == 3:
            dateoffset = current - relativedelta(hours=+offset)
        elif attr == "Minutes" or attr == 4:
            dateoffset = current - relativedelta(minutes=+offset)
        elif attr == "Seconds" or attr == 5:
            dateoffset = current - relativedelta(seconds=+offset)
        elif attr == "Microseconds" or attr == 6:
            dateoffset = current - relativedelta(microseconds=+offset)
        else:
            raise Exception('输入的属性不正确')
    else:
        raise Exception('输入的模式不正确')
    return dateoffset

if __name__ == "__main__":
    two_mon_ago = datetime.date.today() - relativedelta(months=+2)
    print(two_mon_ago)
    print(type(two_mon_ago))
    # print(getdate('Month', 'sub', 2))
    # try:
    #     print(getdate('Month', 'a', 2))
    # except Exception as e:
    #     print(e)


